using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Xml;
using System.Xml.Serialization;
using SubSonic;
using SubSonic.Utilities;

//Generated on 18/07/2007 15:55:03 by rupert_bates

namespace OpenProject.DataObjects{
    /// <summary>
    /// Strongly-typed collection for the Role class.
    /// </summary>
    [Serializable]
    public partial class RoleCollection : ActiveList<Role, RoleCollection> 
    {       
        public RoleCollection() {}

    }

    /// <summary>
    /// This is an ActiveRecord class which wraps the Role table.
    /// </summary>
    [Serializable]
    public partial class Role : ActiveRecord<Role>
    {
    
	    #region Default Settings
	    protected static void SetSQLProps() 
	    {
		    GetTableSchema();
	    }

	    #endregion
        #region Schema Accessor
	    public static TableSchema.Table Schema
        {
            get
            {
                if (BaseSchema == null)
                {
                    SetSQLProps();
                }

                return BaseSchema;
            }

        }

    	
        private static void GetTableSchema() 
	    {
            if(!IsSchemaInitialized)
            {
                //Schema declaration
				TableSchema.Table schema = new TableSchema.Table("Role", TableType.Table, DataService.GetInstance("OpenProject"));
                schema.Columns = new TableSchema.TableColumnCollection();
                schema.SchemaName = @"dbo";
                //columns
                
                TableSchema.TableColumn colvarRoleId = new TableSchema.TableColumn(schema);
                colvarRoleId.ColumnName = "RoleId";
                colvarRoleId.DataType = DbType.Int32;
                colvarRoleId.MaxLength = 0;
                colvarRoleId.AutoIncrement = true;
                colvarRoleId.IsNullable = false;
                colvarRoleId.IsPrimaryKey = true;
                colvarRoleId.IsForeignKey = false;
                colvarRoleId.IsReadOnly = false;
                
                
                schema.Columns.Add(colvarRoleId);
                
                TableSchema.TableColumn colvarRoleName = new TableSchema.TableColumn(schema);
                colvarRoleName.ColumnName = "RoleName";
                colvarRoleName.DataType = DbType.String;
                colvarRoleName.MaxLength = 256;
                colvarRoleName.AutoIncrement = false;
                colvarRoleName.IsNullable = false;
                colvarRoleName.IsPrimaryKey = false;
                colvarRoleName.IsForeignKey = false;
                colvarRoleName.IsReadOnly = false;
                
                
                schema.Columns.Add(colvarRoleName);
                
                TableSchema.TableColumn colvarLoweredRoleName = new TableSchema.TableColumn(schema);
                colvarLoweredRoleName.ColumnName = "LoweredRoleName";
                colvarLoweredRoleName.DataType = DbType.String;
                colvarLoweredRoleName.MaxLength = 256;
                colvarLoweredRoleName.AutoIncrement = false;
                colvarLoweredRoleName.IsNullable = false;
                colvarLoweredRoleName.IsPrimaryKey = false;
                colvarLoweredRoleName.IsForeignKey = false;
                colvarLoweredRoleName.IsReadOnly = false;
                
                
                schema.Columns.Add(colvarLoweredRoleName);
                
                TableSchema.TableColumn colvarDescription = new TableSchema.TableColumn(schema);
                colvarDescription.ColumnName = "Description";
                colvarDescription.DataType = DbType.String;
                colvarDescription.MaxLength = 256;
                colvarDescription.AutoIncrement = false;
                colvarDescription.IsNullable = true;
                colvarDescription.IsPrimaryKey = false;
                colvarDescription.IsForeignKey = false;
                colvarDescription.IsReadOnly = false;
                
                
                schema.Columns.Add(colvarDescription);
                
                BaseSchema = schema;
                //add this schema to the provider
                //so we can query it later
                DataService.Providers["OpenProject"].AddSchema("Role",schema);
            }

        }

        #endregion
        
        #region Query Accessor
	    public static Query CreateQuery()
	    {
		    return new Query(Schema);
	    }

	    #endregion
	    
	    #region .ctors
	    public Role()
	    {
            SetSQLProps();
            SetDefaults();
            MarkNew();
        }

        
        public Role(bool useDatabaseDefaults)
	    {
		    SetSQLProps();
		    if(useDatabaseDefaults)
		    {
				ForceDefaults();
			}

			MarkNew();
	    }

	    public Role(object keyID)
	    {
		    SetSQLProps();
            SetDefaults();
		    LoadByKey(keyID);
	    }

    	 
	    public Role(string columnName, object columnValue)
        {
            SetSQLProps();
            SetDefaults();
            LoadByParam(columnName,columnValue);
        }

        
	    #endregion
	    
	    #region Props
	    
          
        [XmlAttribute("RoleId")]
        public int RoleId 
	    {
		    get { return GetColumnValue<int>("RoleId"); }

            set 
		    {
			    SetColumnValue("RoleId", value);
            }

        }

	      
        [XmlAttribute("RoleName")]
        public string RoleName 
	    {
		    get { return GetColumnValue<string>("RoleName"); }

            set 
		    {
			    SetColumnValue("RoleName", value);
            }

        }

	      
        [XmlAttribute("LoweredRoleName")]
        public string LoweredRoleName 
	    {
		    get { return GetColumnValue<string>("LoweredRoleName"); }

            set 
		    {
			    SetColumnValue("LoweredRoleName", value);
            }

        }

	      
        [XmlAttribute("Description")]
        public string Description 
	    {
		    get { return GetColumnValue<string>("Description"); }

            set 
		    {
			    SetColumnValue("Description", value);
            }

        }

	    
	    #endregion
	    
	    
	    #region PrimaryKey Methods
	    
		public OpenProject.DataObjects.UserRoleCollection UserRoleRecords
		{
			get
			{
				return new OpenProject.DataObjects.UserRoleCollection().Where(UserRole.Columns.RoleId, RoleId).Load();
			}

		}

		#endregion
		
	 	
			
	    
	    //no foreign key tables defined (0)
	    
	    
	    
	    #region Many To Many Helpers
	    
	     
        public OpenProject.DataObjects.UserMembershipCollection GetUserMembershipCollection() {
            return Role.GetUserMembershipCollection(this.RoleId);
        }

        public static OpenProject.DataObjects.UserMembershipCollection GetUserMembershipCollection(int varRoleId) {
            SubSonic.QueryCommand cmd = new SubSonic.QueryCommand(
                "SELECT * FROM UserMembership INNER JOIN UserRole ON "+
                "UserMembership.UserId=UserRole.UserId WHERE UserRole.RoleId=@RoleId", Role.Schema.Provider.Name);
            
            cmd.AddParameter("@RoleId", varRoleId, DbType.Int32);
            
            IDataReader rdr = SubSonic.DataService.GetReader(cmd);
            UserMembershipCollection coll = new UserMembershipCollection();
            coll.LoadAndCloseReader(rdr);
            return coll;
        }

        public static void SaveUserMembershipMap(int varRoleId, UserMembershipCollection items) {
            
            QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
            //delete out the existing
            QueryCommand cmdDel = new QueryCommand("DELETE FROM UserRole WHERE RoleId=@RoleId", Role.Schema.Provider.Name);
            cmdDel.AddParameter("@RoleId", varRoleId);
            //add this in
            coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
            foreach (UserMembership item in items)
            {
				UserRole varUserRole = new UserRole();
				varUserRole.SetColumnValue("RoleId", varRoleId);
				varUserRole.SetColumnValue("UserId", item.GetPrimaryKeyValue());
				varUserRole.Save();
            }

        }

        public static void SaveUserMembershipMap(int varRoleId, System.Web.UI.WebControls.ListItemCollection itemList) 
        {
            QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
            //delete out the existing
             QueryCommand cmdDel = new QueryCommand("DELETE FROM UserRole WHERE RoleId=@RoleId", Role.Schema.Provider.Name);
            cmdDel.AddParameter("@RoleId", varRoleId);
            //add this in
            coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
            foreach (System.Web.UI.WebControls.ListItem l in itemList) 
            {
                if (l.Selected) 
                {
					UserRole varUserRole = new UserRole();
					varUserRole.SetColumnValue("RoleId", varRoleId);
					varUserRole.SetColumnValue("UserId", l.Value);
					varUserRole.Save();
                }

            }

        }

        public static void SaveUserMembershipMap(int varRoleId , int[] itemList) 
        {
            QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
            //delete out the existing
             QueryCommand cmdDel = new QueryCommand("DELETE FROM UserRole WHERE RoleId=@RoleId", Role.Schema.Provider.Name);
            cmdDel.AddParameter("@RoleId", varRoleId);
            //add this in
            coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
            foreach (int item in itemList) 
            {
            	UserRole varUserRole = new UserRole();
				varUserRole.SetColumnValue("RoleId", varRoleId);
				varUserRole.SetColumnValue("UserId", item);
				varUserRole.Save();
            }

        }

        
        public static void DeleteUserMembershipMap(int varRoleId) 
        {
            QueryCommand cmdDel = new QueryCommand("DELETE FROM UserRole WHERE RoleId=@RoleId", Role.Schema.Provider.Name);
            cmdDel.AddParameter("@RoleId", varRoleId);
            DataService.ExecuteQuery(cmdDel);
		}

	    
	    #endregion
	    
	    #region ObjectDataSource support
    	
    	
	    /// <summary>
	    /// Inserts a record, can be used with the Object Data Source
	    /// </summary>
	    public static void Insert(string varRoleName,string varLoweredRoleName,string varDescription)
	    {
		    Role item = new Role();
		    
            item.RoleName = varRoleName;
            
            item.LoweredRoleName = varLoweredRoleName;
            
            item.Description = varDescription;
            
	    
		    if (System.Web.HttpContext.Current != null)
			    item.Save(System.Web.HttpContext.Current.User.Identity.Name);
		    else
			    item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
	    }

    	
	    /// <summary>
	    /// Updates a record, can be used with the Object Data Source
	    /// </summary>
	    public static void Update(int varRoleId,string varRoleName,string varLoweredRoleName,string varDescription)
	    {
		    Role item = new Role();
		    
                item.RoleId = varRoleId;
				
                item.RoleName = varRoleName;
				
                item.LoweredRoleName = varLoweredRoleName;
				
                item.Description = varDescription;
				
		    item.IsNew = false;
		    if (System.Web.HttpContext.Current != null)
			    item.Save(System.Web.HttpContext.Current.User.Identity.Name);
		    else
			    item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
	    }

	    #endregion
	    #region Columns Struct
	    public struct Columns
	    {
		    
		    
            public static string RoleId = @"RoleId";
            
            public static string RoleName = @"RoleName";
            
            public static string LoweredRoleName = @"LoweredRoleName";
            
            public static string Description = @"Description";
            
	    }

	    #endregion
    }

}

